package _interview100;

import _object.TreeNode;

/**
 * 104. 二叉树的最大深度
 */
public class No104 {
    /**
     * 1.递归
     */
    public int maxDepth(TreeNode root) {
        if (root == null) return 0;
        int leftLen = maxDepth(root.left);
        int rightLen = maxDepth(root.right);
        return Math.max(leftLen, rightLen) + 1;
    }

    /**
     * 2.将数值传递下去
     */
    public int maxDepth2(TreeNode root) {
        return dfs(root, 0);
    }

    private int dfs(TreeNode root, int curLen) {
        if (root == null) return curLen;
        curLen++;
        int leftLen = dfs(root.left, curLen);
        int rightLen = dfs(root.right, curLen);
        return Math.max(leftLen, rightLen);
    }
}
